<template>
  <div>
    <div class="pageTitleWhole">
      <a-breadcrumb>
        <a-breadcrumb-item>交易类型字典</a-breadcrumb-item>
      </a-breadcrumb>
    </div>

    <div class="pageContentWhole">
      <a-row>
        <a-col :span="24">
          <a-form layout="inline">
            <a-form-item label="交易类型">
              <a-input v-model="searchForm.name"></a-input>
            </a-form-item>
            <a-form-item label="状态">
              <a-select v-model="searchForm.status" class="selectWidth">
                <a-select-option key>全部</a-select-option>
                <a-select-option :key="1">正常</a-select-option>
                <a-select-option :key="0">冻结</a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item>
              <a-button type="primary" @click="handleSubmit1">查询</a-button>
            </a-form-item>
            <a-form-item>
              <a-button type="primary" @click="addItem">+  创建类型</a-button>
            </a-form-item>
          </a-form>

          <a-table :columns="columns" :data-source="tableData" :loading="loading" class="tableMargin" :pagination="pagination" rowKey="dictionaryId">
            <template slot="rn" slot-scope="text, record, index">{{index + 1}}</template>
            <template slot="operation" slot-scope="text, record">
                <a @click="editIt(record)">编辑</a>
              </template>
          </a-table>
        </a-col>
      </a-row>
    </div>

    <!-- 添加/编辑 -->
    <a-modal :visible="visible" :title="title" :confirmLoading="confirmLoading"
      :destroyOnClose="true" :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      >
      <a-form-model
        ref="rulesForm"
        :model="form" :label-col="labelCol" :wrapper-col="wrapperCol"
        :rules="rules">
        <a-form-model-item label="交易类型名称" prop="name">
          <a-input v-model="form.name" :max-length="20"></a-input>
        </a-form-model-item>
        <a-form-model-item label="状态">
          <a-select v-model="form.status">
            <a-select-option :key="1">正常</a-select-option>
            <a-select-option :key="0">冻结</a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import { queryDataDictionaryList, addDataDictionary } from '@/api'

const fomrMessage = `必填项不能为空`
const defaultForm = () => {
  return {
    dictionaryId: null,
    type: 'TRANSACTION_TYPE',
    name: '',
    status: 1
  }
}

export default {
  data () {
    const columns = [
      {
        title: '序号',
        width: 60,
        scopedSlots: {customRender: 'rn'}
      },
      {
        title: '交易类型',
        dataIndex: 'name'
      },
      {
        title: '状态',
        dataIndex: 'status',
        customRender: (text, row, index) => {
          if (text === 1) {
            return <a-badge status="success" text="正常"/>
          } else if (text === 0) {
            return <a-badge status="error" text="冻结"/>
          }
        }
      },
      {
        title: '操作',
        width: 90,
        scopedSlots: {customRender: 'operation'}
      }
    ]

    return {
      searchForm: {
        type: 'TRANSACTION_TYPE',
        name: '',
        status: '' // 状态：1正常，0冻结
      },
      columns,
      tableData: [],
      loading: false,
      pagination: {},
      pageNum: 1,
      pageSize: 10,
      // modal
      labelCol: {span: 8},
      wrapperCol: {span: 14},
      visible: false,
      title: '',
      confirmLoading: false,
      form: defaultForm(),
      rules: {
        name: [{required: true, message: fomrMessage, trigger: 'blur'}]
      }
    }
  },
  methods: {
    handleSubmit1 () {
      this.pageNum = 1
      this.handleSubmit()
    },
    handleSubmit () {
      this.loading = true
      queryDataDictionaryList({
        ...this.searchForm,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }).then(rsp => {
        this.loading = false
        this.tableData = rsp.data.list
        this.pagination = {
          current: this.pageNum,
          pageSize: rsp.data.pageSize,
          total: rsp.data.total,
          showQuickJumper: false,
          showSizeChanger: true,
          showTotal: total => `共${total}条记录 第${this.pageNum}/${rsp.data.pages}页`,
          onChange: pageNum => {
            this.pageNum = pageNum
            this.handleSubmit()
          },
          onShowSizeChange: (current, size) => {
            this.pageNum = current
            this.pageSize = size
            this.handleSubmit()
          }
        }
      }).catch(() => { this.loading = false })
    },
    // 添加
    addItem () {
      if (!this.$root._bc.create) {
        this.$message.error('您无此权限！')
        return
      }
      this.form = defaultForm()
      this.title = '创建'
      this.visible = true
    },
    // 编辑
    editIt (item) {
      if (!this.$root._bc.edit) {
        this.$message.error('您无此权限！')
        return
      }
      this.form = {...item, type: 'TRANSACTION_TYPE'}
      this.title = '编辑'
      this.visible = true
    },
    handleOk () {
      this.$refs.rulesForm.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          addDataDictionary(this.form).then(rsp => {
            this.$message.success('操作成功')
            this.visible = false
            this.handleSubmit()
            setTimeout(() => { this.confirmLoading = false }, 1000)
          }).catch(() => { this.confirmLoading = false })
        }
      })
    },
    handleCancel () {
      this.visible = false
    },
    detailIt (id) {
      this.$router.push('fundAccountManagement/' + id)
    }
  },
  mounted () {
    this.handleSubmit()
  }
}
</script>

<style lang="less" scoped>
</style>
